﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// using Chat;

namespace Chat.Client.GUI
{
    public partial class ConnectToDialog : Chat.GUI.IPEndPointDialog
    {
        public ConnectToDialog()
        {
            InitializeComponent();
        }

        private LoginData login;

        public LoginData Login
        {
            get { return login; }
            protected set { login = value; }
        }

        protected override bool ValidateData()
        {
            bool isError = !base.ValidateData();

            NickName nick = null;

            try
            {
                nick = new NickName(this.txtNickName.Text);
                ClearError(this.txtNickName);
            }
            catch (NickNameException ex)
            {
                isError = true;
                ShowErrorMsg(this.txtNickName, ex.Message);
            }

            return !isError;
        }

        protected override void UpdateModel()
        {
 	        base.UpdateModel();

            NickName nick = new NickName(this.txtNickName.Text);

            if (Login == null)
            {
                Login = new LoginData(nick, this.colorDialog.Color);
            }
            else
            {
                Login.User = nick;
                Login.TextColor = this.colorDialog.Color;
            }
        }

        protected override bool RestoreFromModel()
        {
            if (!base.RestoreFromModel())
                return false;

            if (Login == null)
                return false;
            
            this.txtNickName.Text = Login.User.Value;
            this.colorDialog.Color = Login.TextColor;

            return true;
        }

        private void btnSelectColor_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.colorDialog.ShowDialog();

            if (dr == DialogResult.OK)
            {
                this.lblAaBbCcXxYyZz.ForeColor = this.colorDialog.Color;
            }
        }
    }
}
